package com.hCode.CnSmart.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hCode.CnSmart.service.SysRoleService;
import com.hCode.CnSmart.entity.SysRole;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;



import java.util.List;

/**
* @author Xiao
* @since 2020-11-10
*/
@Slf4j
@Api(tags = {""})
@RestController
@RequestMapping("/sysRole")
public class SysRoleController {
    
    @Autowired
    public SysRoleService sysRoleService;

    @ApiOperation(value = "新增")
    @PostMapping("/save")
    public Object save(@RequestBody SysRole sysRole){
        boolean b = sysRoleService.save(sysRole);
        return b;
    }

    @ApiOperation(value = "根据id删除")
    @PostMapping("/delete/{id}")
    public Object delete(@PathVariable("id") Long id){
        boolean b = sysRoleService.removeById(id);
        return b;
    }

    @ApiOperation(value = "批量删除")
    @PostMapping("/delete/batch")
    public Object deleteBatch(@RequestBody String ids){
        List<Long> idList = (List<Long>)JSON.parse(ids);
        boolean b = sysRoleService.removeByIds(idList);
        return b;
    }

    @ApiOperation(value = "列表（分页）")
    @GetMapping("/list")
    public Object list(Long pageNum, Long pageSize){
        IPage<SysRole> page = sysRoleService.page(
                new Page<>(pageNum, pageSize),
                new LambdaQueryWrapper<SysRole>().eq(false, SysRole::getId, 0));
        return page;
    }

    @ApiOperation(value = "详情")
    @GetMapping("/get/{id}")
    public Object get(@PathVariable("id") Long id){
        SysRole sysRole = sysRoleService.getById(id);
        return sysRole;
    }

	/*
	 * @ApiOperation(value = "根据id修改")
	 * 
	 * @PostMapping("/update/{id}") public Object update(@PathVariable("id") Long
	 * id, @RequestBody SysRole sysRole){ sysRole.setId(id); boolean b =
	 * sysRoleService.updateById(sysRole); return b; }
	 */

    @ApiOperation(value = "批量修改")
    @PostMapping("/update/batch")
    public Object updateBatch(@RequestBody String sysRoles){
        List<SysRole> sysRoleList = (List<SysRole>)JSON.parse(sysRoles);
        boolean b = sysRoleService.updateBatchById(sysRoleList);
        return b;
    }

}